//
//  AlibabaCloudPushPlugin.m
//  MyApp
//
//  Created by apple on 2017/6/20.
//
//

#import "AlibabaCloudPushPlugin.h"
#import "AppDelegate.h"
#import <Cordova/CDV.h>
#import <CloudPushSDK/CloudPushSDK.h>

@interface AlibabaCloudPushPlugin()

@property(nonatomic) NSString *receiveCallbackId ;
@property(nonatomic) NSString *openCallbackId ;


@end

@implementation AlibabaCloudPushPlugin

- (void)pluginInitialize {
    
}

- (void)registerAccount:(CDVInvokedUrlCommand*)command {
    NSArray *array = command.arguments ;
    NSString *account = @"" ;
    if (array && array.count > 0) {
        account = array[0] ;
    }
    [CloudPushSDK bindAccount:account withCallback:^(CloudPushCallbackResult *res) {
        CDVPluginResult *pluginResult = nil;
        if (res.success) {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:@{@"data":res.data ? res.data : @{}}];
        } else {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:@{@"error":res.error ? res.error : @""}];
        }
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    }];
}

- (void)unRegisterAccount:(CDVInvokedUrlCommand*)command {
    [CloudPushSDK unbindAccount:^(CloudPushCallbackResult *res) {
        CDVPluginResult *pluginResult = nil;
        if (res.success) {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:@{@"data":res.data ? res.data : @{}}];
        } else {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:@{@"error":res.error ? res.error : @""}];
        }
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    }];
}

- (void)registerTag:(CDVInvokedUrlCommand*)command {
    NSArray *array = command.arguments ;
    NSString *tag = @"" ;
    if (array && array.count > 0) {
        tag = array[0] ;
    }
    [CloudPushSDK bindTag:1 withTags:@[tag] withAlias:nil withCallback:^(CloudPushCallbackResult *res) {
        CDVPluginResult *pluginResult = nil;
        if (res.success) {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:@{@"data":res.data ? res.data : @{}}];
        } else {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:@{@"error":res.error ? res.error : @""}];
        }
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    }];
}

- (void)unRegisterTag:(CDVInvokedUrlCommand*)command {
    NSArray *array = command.arguments ;
    NSString *tag = @"" ;
    if (array && array.count > 0) {
        tag = array[0] ;
    }
    [CloudPushSDK unbindTag:1 withTags:@[tag] withAlias:nil withCallback:^(CloudPushCallbackResult *res) {
        CDVPluginResult *pluginResult = nil;
        if (res.success) {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:@{@"data":res.data ? res.data : @{}}];
        } else {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:@{@"error":res.error ? res.error : @""}];
        }
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    }];
    
}

//查询标签 target默认1
- (void)listTags:(CDVInvokedUrlCommand*)command {
    [CloudPushSDK listTags:1 withCallback:^(CloudPushCallbackResult *res) {
        CDVPluginResult *pluginResult = nil;
        if (res.success) {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:@{@"data":res.data ? res.data : @{}}];
        } else {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:@{@"error":res.error ? res.error : @""}];
        }
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    }];
}

- (void)registerAlias:(CDVInvokedUrlCommand*)command {
    NSArray *array = command.arguments ;
    NSString *alias = @"" ;
    if (array && array.count > 0) {
        alias = array[0] ;
    }
    [CloudPushSDK addAlias:alias withCallback:^(CloudPushCallbackResult *res) {
        CDVPluginResult *pluginResult = nil;
        if (res.success) {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:@{@"data":res.data ? res.data : @{}}];
        } else {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:@{@"error":res.error ? res.error : @""}];
        }
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    }];
}

- (void)unRegisterAlias:(CDVInvokedUrlCommand*)command {
    NSArray *array = command.arguments ;
    NSString *alias = @"" ;
    if (array && array.count > 0) {
        alias = array[0] ;
    }
    [CloudPushSDK removeAlias:alias withCallback:^(CloudPushCallbackResult *res) {
        CDVPluginResult *pluginResult = nil;
        if (res.success) {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:@{@"data":res.data ? res.data : @{}}];
        } else {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:@{@"error":res.error ? res.error : @""}];
        }
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    }];
}

- (void)listAliases:(CDVInvokedUrlCommand*)command {
    [CloudPushSDK listAliases:^(CloudPushCallbackResult *res) {
        CDVPluginResult *pluginResult = nil;
        if (res.success) {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:@{@"data":res.data ? res.data : @{}}];
        } else {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:@{@"error":res.error ? res.error : @""}];
        }
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    }];
}

- (void)setLocalNotification:(CDVInvokedUrlCommand*)command {
    
}

- (void)deleteLocalNotificationWithIdentifierKey:(CDVInvokedUrlCommand*)command {
    
}

- (void)clearAllLocalNotifications:(CDVInvokedUrlCommand*)command {
    
}

- (void)getApplicationIconBadgeNumber:(CDVInvokedUrlCommand*)command {
    NSNumber *number = @([UIApplication sharedApplication].applicationIconBadgeNumber) ;
    CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:@{@"data":number ? number : @0}];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void)setApplicationIconBadgeNumber:(CDVInvokedUrlCommand*)command {
    NSArray *array = command.arguments ;
    NSNumber *number = 0 ;
    if (array && array.count > 0) {
        number = array[0] ;
    }
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:[number integerValue]];
}

- (void)getUserNotificationSettings:(CDVInvokedUrlCommand*)command {
    
}

- (void)requestPermission:(CDVInvokedUrlCommand*)command {
    
}

- (void)receiveNotificationObservable:(CDVInvokedUrlCommand*)command {
    
    self.receiveCallbackId = command.callbackId ;
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"RECEIVE_NOTIFICATION" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"RECEIVE_NOTIFICATION" object:nil];
    
//    AlibabaCloudPushPlugin *testclass = [[AlibabaCloudPushPlugin alloc]init];
    AppDelegate *notificationInfoDelegete = [UIApplication sharedApplication].delegate;
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"进入if上面" delegate:self cancelButtonTitle:notificationInfoDelegete.notificationInfo[@"title"] otherButtonTitles:@"123", nil];
//    [alert show];
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"RECEIVE_NOTIFICATION" object:nil userInfo:notificationInfoDelegete.notificationInfo];
    
//    NSError * error = nil;
//    NSData * jsonData = [NSJSONSerialization dataWithJSONObject:notificationInfoDelegete.notificationInfo options:NSJSONWritingPrettyPrinted error:&error];
//    NSString * jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:jsonStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"这里是正经的alert",notificationInfoDelegete.notificationInfo[@"content"], nil];
//    [alert show];
    
//
//    if(notificationInfoDelegete.notificationInfo[@"title"]||notificationInfoDelegete.notificationInfo[@"content"]){
//                NSError * error = nil;
//                NSData * jsonData = [NSJSONSerialization dataWithJSONObject:notificationInfoDelegete.notificationInfo options:NSJSONWritingPrettyPrinted error:&error];
//                NSString * jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
//                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:jsonStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"注册接受",notificationInfoDelegete.notificationInfo[@"content"], nil];
//                    [alert show];
//
//        notificationInfoDelegete.notificationInfo[@"title"] = nil;
//        notificationInfoDelegete.notificationInfo[@"content"] = nil;
//    }
}

-(void) receiveNotification:(NSNotification *)notification {
    
//    NSError * error = nil;
//    NSData * jsonData = [NSJSONSerialization dataWithJSONObject:notification.userInfo options:NSJSONWritingPrettyPrinted error:&error];
//    NSString * jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:jsonStr delegate:self cancelButtonTitle:@"fasong cordova" otherButtonTitles:@"打开app进行初始化", notification.userInfo[@"data"], nil];
//    [alert show];
    
    CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary: notification.userInfo];
    [pluginResult setKeepCallbackAsBool:YES];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:self.receiveCallbackId];
}

- (void)openNotificationObservable:(CDVInvokedUrlCommand*)command {
    self.openCallbackId = command.callbackId ;
    AppDelegate *notificationInfoDelegete = [UIApplication sharedApplication].delegate;
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"OPEN_NOTIFICATION" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(openNotification:) name:@"OPEN_NOTIFICATION" object:nil];

//    [[NSNotificationCenter defaultCenter] postNotificationName:@"RECEIVE_NOTIFICATION" object:nil userInfo:notificationInfoDelegete.notificationInfo];
    if(notificationInfoDelegete.notificationInfo[@"title"]||notificationInfoDelegete.notificationInfo[@"content"]){
//        NSError * error = nil;
//        NSData * jsonData = [NSJSONSerialization dataWithJSONObject:notificationInfoDelegete.notificationInfo options:NSJSONWritingPrettyPrinted error:&error];
//        NSString * jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:jsonStr delegate:self cancelButtonTitle:@"fasong cordova get open" otherButtonTitles:@"打开app进行初始化", notificationInfoDelegete.notificationInfo[@"title"], nil];
//        [alert show];

        [[NSNotificationCenter defaultCenter] postNotificationName:@"OPEN_NOTIFICATION" object:nil userInfo:notificationInfoDelegete.notificationInfo];
        notificationInfoDelegete.notificationInfo[@"title"] = nil;
        notificationInfoDelegete.notificationInfo[@"content"] = nil;
    }
}

-(void) openNotification:(NSNotification *)notification {
//    NSError * error = nil;
//    NSData * jsonData = [NSJSONSerialization dataWithJSONObject:notification.userInfo options:NSJSONWritingPrettyPrinted error:&error];
//    NSString * jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:jsonStr delegate:self cancelButtonTitle:@"fasong cordova open" otherButtonTitles:@"打开app进行初始化", notification.userInfo[@"data"], nil];
//    [alert show];
    
    CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:notification.userInfo];
    [pluginResult setKeepCallbackAsBool:YES];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:self.openCallbackId];
}





@end
